<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserAchievement extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'achievement_key', 'achievement_name', 'description'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'unlocked_at' => 'datetime',
    ];

    /**
     * 所属用户关系
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 获取成就类型
     */
    public function getCategoryAttribute()
    {
        $categories = [
            'social_newbie' => 'training',
            'ice_breaker' => 'skill',
            'seven_day_warrior' => 'consistency',
            'empathy_master' => 'skill',
            'conversation_catalyst' => 'training',
            'perfect_score' => 'excellence',
            'social_butterfly' => 'exploration',
            'helping_hand' => 'community',
        ];

        return $categories[$this->achievement_key] ?? 'general';
    }

    /**
     * 获取成就难度
     */
    public function getDifficultyAttribute()
    {
        $difficulties = [
            'social_newbie' => 'easy',
            'ice_breaker' => 'hard',
            'seven_day_warrior' => 'medium',
            'empathy_master' => 'hard',
            'conversation_catalyst' => 'medium',
            'perfect_score' => 'expert',
            'social_butterfly' => 'medium',
            'helping_hand' => 'medium',
        ];

        return $difficulties[$this->achievement_key] ?? 'medium';
    }

    /**
     * 检查是否为里程碑成就
     */
    public function isMilestone()
    {
        $milestones = ['ice_breaker', 'seven_day_warrior', 'perfect_score'];
        return in_array($this->achievement_key, $milestones);
    }
}